package com.tangdi.production.mpaychl.service.impl.kuaiqian;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tangdi.production.mpaychl.base.service.BaseTranService;
import com.tangdi.production.mpaychl.service.impl.kuaiqian.batchPayWS.BatchPayServiceLocator;
import com.tangdi.production.mpaychl.service.impl.kuaiqian.complatible.BankRequestBean;
import com.tangdi.production.mpaychl.service.impl.kuaiqian.complatible.BankResponseBean;
import com.tangdi.production.mpaychl.service.impl.kuaiqian.complatible.QueryRequestBean;
import com.tangdi.production.mpaychl.service.impl.kuaiqian.complatible.QueryResponseBean;
import com.tangdi.production.mpaychl.service.impl.kuaiqian.gatewayOrderQuery.GatewayOrderQueryServiceLocator;
import com.tangdi.production.mpaychl.service.impl.kuaiqian.gatewayquery.GatewayOrderDetail;
import com.tangdi.production.mpaychl.service.impl.kuaiqian.gatewayquery.GatewayOrderQueryRequest;
import com.tangdi.production.mpaychl.service.impl.kuaiqian.gatewayquery.GatewayOrderQueryResponse;
import com.tangdi.production.mpaychl.service.impl.kuaiqian.md5.MD5Util;
import com.tangdi.production.mpbase.constants.ExcepCode;
import com.tangdi.production.mpbase.constants.MsgCT;
import com.tangdi.production.mpbase.exception.BusinessException;
import com.tangdi.production.mpbase.exception.TranException;
import com.tangdi.production.mpbase.service.UnionfitService;
import com.tangdi.production.mpbase.util.ConfigUtils;
import com.tangdi.production.mpbase.util.MapUtils;
import com.tangdi.production.mpbase.util.MoneyUtils;
import com.tangdi.production.mpbase.util.ObjectUtil;
import com.tangdi.production.mpbase.util.ParamValidate;

/**
 * 快钱
 * 
 * @author HarryNie
 *
 */
@SuppressWarnings("unused")
@Service("QUICK_KuaiQian")
public class KuaiQianServiceImpl implements BaseTranService {
	private static Logger log = LoggerFactory
			.getLogger(KuaiQianServiceImpl.class);

	private static Properties myProp = ConfigUtils
			.loadProperties("mpaychl/conf-chl-properties/kuaiqian.properties");

	private String charset = "UTF-8";

	@Autowired
	private UnionfitService unionfitService;

	@Override
	public Map<String, Object> execute(Map<String, Object> param)
			throws TranException {
		log.info("进入自有跳转处理接口 [{}]", param);
		// 路由编码
		String rtrcod = (String) param.get("rtrcod");
		String nodeId = rtrcod;

		try {
			// 公共参数设置
			if ("gateway_pay".equals(rtrcod) || "pay".equals(rtrcod)) {// 下单
				return pay(param);
			} else if ("pay_query".equals(rtrcod) || "pay_que".equals(rtrcod)) {// 交易查询
				return queryPayStatus2(param);
			} else if ("withdrawal".equals(rtrcod)) {
				return withdrawal(param);
			} else if ("withdrawalQuery".equals(rtrcod)) {// 代付交易查询
				return withdrawalQuery(param); 
			} else {
				throw new BusinessException(ExcepCode.EX900003, "不支持的路由编码[{}]",
						rtrcod);
			}
		} catch (TranException e) {
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("渠道调用异常:" + e.getMessage(), e);
			throw new BusinessException(ExcepCode.EX900001, "执行出错");
		}
		// return new HashMap<String, Object>();
	}

	/**
	 * 辅助类
	 * 
	 * @param returns
	 * @param paramId
	 * @param paramValue
	 * @return
	 */
	public String appendParam(String returns, String paramId, String paramValue) {
		if (returns != "") {
			if (paramValue != "") {
				returns += "&" + paramId + "=" + paramValue;
			}
		} else {
			if (paramValue != "") {
				returns = paramId + "=" + paramValue;
			}
		}
		return returns;
	}

	/**
	 * 快钱下单
	 * 
	 * @param param
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> pay(Map<String, Object> param) throws Exception {
		log.info("[快钱]下单接口:[{}]", param);
		ParamValidate.doing(param, "TMercId", "prdOrdNo", "payAmt");
		Map<String, Object> backMap = new HashMap<String, Object>();
		backMap.put("ordStatus", MsgCT.ORDSTATUS_NO_PAY);

		// String signKey = (String) param.get("merKey");
		String TMercId = (String) param.get("TMercId");
		// 固定参数
		String ordName = ""; // 订单名称（描述）
		String payAmt = ObjectUtil.getString(param.get("payAmt"));
		LinkedHashMap<String, String> datamap = new LinkedHashMap<String, String>(); // data参数的map

		String rateType = ObjectUtil.getString(param.get("rateType"));
		log.info("费率类型[{}]", rateType);

		// 快钱 start
		String merchantAcctId = TMercId + "01"; // 人民币网关账号，该账号为11位人民币网关商户编号+01,该参数必填。
		String inputCharset = "1"; // 编码方式，1代表 UTF-8; 2 代表 GBK; 3代表 GB2312
									// 默认为1,该参数必填。
		String pageUrl = ""; // 接收支付结果的页面地址，该参数一般置为空即可。
		String bgUrl = myProp.getProperty("pay_call_back_url"); // 服务器接收支付结果的后台地址，该参数务必填写，不能为空。
		String version = "v2.0"; // 网关版本，固定值：v2.0,该参数必填。
		String language = "1"; // 语言种类，1代表中文显示，2代表英文显示。默认为1,该参数必填。
		String signType = "4"; // 签名类型,该值为4，代表PKI加密方式,该参数必填。
		String payerName = ObjectUtil.getString(param.get("custName")); // 支付人姓名,可以为空。
		String payerContactType = "2"; // 支付人联系类型，1 代表电子邮件方式；2 代表手机联系方式。可以为空。
		String payerContact = ObjectUtil.getString(param.get("usrMobile")); // 支付人联系方式，与payerContactType设置对应，payerContactType为1，则填写邮箱地址；payerContactType为2，则填写手机号码。可以为空。
		String orderId = ObjectUtil.getString(param.get("prdOrdNo")); // 商户订单号，以下采用时间来定义订单号，商户可以根据自己订单号的定义规则来定义该值，不能为空。
		String orderAmount = payAmt; // 订单金额，金额以“分”为单位，商户测试以1分测试即可，切勿以大金额测试。该参数必填。
		String orderTime = new java.text.SimpleDateFormat("yyyyMMddHHmmss")
				.format(new Date()); // 订单提交时间，格式：yyyyMMddHHmmss，如：20071117020101，不能为空。
		String productName = ""; // 商品名称，可以为空。
		String productNum = ""; // 商品数量，可以为空。
		String productId = ""; // 商品代码，可以为空。
		String productDesc = ""; // 商品描述，可以为空。
		String ext1 = ObjectUtil.getString(param.get("frontUrl")); // 扩展字段1，商户可以传递自己需要的参数，支付完快钱会原值返回，可以为空。
		String ext2 = ""; // 扩展自段2，商户可以传递自己需要的参数，支付完快钱会原值返回，可以为空。
		String payType = "10-1"; // 支付方式，一般为00，代表所有的支付方式。如果是银行直连商户，该值为10，必填。
		String bankId = ""; // 银行代码，如果payType为00，该值可以为空；如果payType为10，该值必须填写，具体请参考银行列表。

		// 根据提现银行编号获得配置的第三方银行编号
		String bankCode = (String) param.get("bankCode");
		String bankName = "";
		if (StringUtils.isNotEmpty(bankCode)) {
			try {
				Map<String, Object> queryMap = new HashMap<String, Object>();
				queryMap.put("channel", "KuaiQian");
				queryMap.put("binBankCode", bankCode);
				List<Map<String, Object>> thirdBankList = unionfitService
						.getThirdBankInfo(queryMap);
				if (thirdBankList != null && !thirdBankList.isEmpty()) {
					Map<String, Object> thirdBankMap = thirdBankList.get(0);
					bankId = MapUtils.getString(thirdBankMap, "thirdBankCode",
							bankCode);
					bankName = MapUtils.getString(thirdBankMap, "bankName", "");
					log.info("获取第三方银行对照信息完成 [bankCode={}] [bankName={}]",
							bankCode, bankName);
				} else {
					backMap.put("ordStatus", MsgCT.ORDSTATUS_FAIL);
					backMap.put("msg", "暂不支持该银行!");
					backMap.put("busContent", "暂不支持该银行!");
					backMap.put("resJson", "{\"msg\":\"不支持的银行编码[" + bankCode
							+ "]\"}");
					return backMap;
				}
			} catch (Exception e) {
				log.error("获取第三方银行对照信息异常:" + e.getMessage(), e);
			}

		}

		String redoFlag = ""; // 同一订单禁止重复提交标志，实物购物车填1，虚拟产品用0。1代表只能提交一次，0代表在支付不成功情况下可以再提交。可为空。
		String pid = TMercId; // 快钱合作伙伴的帐户号，即商户编号，可为空。
		String signMsgVal = ""; // signMsg 签名字符串 不可空，生成加密签名串
		signMsgVal = appendParam(signMsgVal, "inputCharset", inputCharset);
		signMsgVal = appendParam(signMsgVal, "pageUrl", pageUrl);
		signMsgVal = appendParam(signMsgVal, "bgUrl", bgUrl);
		signMsgVal = appendParam(signMsgVal, "version", version);
		signMsgVal = appendParam(signMsgVal, "language", language);
		signMsgVal = appendParam(signMsgVal, "signType", signType);
		signMsgVal = appendParam(signMsgVal, "merchantAcctId", merchantAcctId);
		signMsgVal = appendParam(signMsgVal, "payerName", payerName);
		signMsgVal = appendParam(signMsgVal, "payerContactType",
				payerContactType);
		signMsgVal = appendParam(signMsgVal, "payerContact", payerContact);
		signMsgVal = appendParam(signMsgVal, "orderId", orderId);
		signMsgVal = appendParam(signMsgVal, "orderAmount", orderAmount);
		signMsgVal = appendParam(signMsgVal, "orderTime", orderTime);
		signMsgVal = appendParam(signMsgVal, "productName", productName);
		signMsgVal = appendParam(signMsgVal, "productNum", productNum);
		signMsgVal = appendParam(signMsgVal, "productId", productId);
		signMsgVal = appendParam(signMsgVal, "productDesc", productDesc);
		signMsgVal = appendParam(signMsgVal, "ext1", ext1);
		signMsgVal = appendParam(signMsgVal, "ext2", ext2);
		signMsgVal = appendParam(signMsgVal, "payType", payType);
		signMsgVal = appendParam(signMsgVal, "bankId", bankId);
		signMsgVal = appendParam(signMsgVal, "redoFlag", redoFlag);
		signMsgVal = appendParam(signMsgVal, "pid", pid);
		Pkipair pki = new Pkipair();
		String signMsg = pki.signMsg(signMsgVal);
		// System.out.println("--------"+signMsg);
		// end

		datamap.put("inputCharset", inputCharset);
		datamap.put("pageUrl", pageUrl);
		datamap.put("bgUrl", bgUrl);
		datamap.put("version", version);
		datamap.put("language", language);
		datamap.put("signType", signType);
		datamap.put("signMsg", signMsg);
		datamap.put("merchantAcctId", merchantAcctId);
		datamap.put("payerName", payerName);
		datamap.put("payerContactType", payerContactType);
		datamap.put("payerContact", payerContact);
		datamap.put("orderId", orderId);
		datamap.put("orderAmount", orderAmount);
		datamap.put("orderTime", orderTime);
		datamap.put("productName", productName);
		datamap.put("productNum", productNum);
		datamap.put("productId", productId);
		datamap.put("productDesc", productDesc);
		datamap.put("ext1", ext1);
		datamap.put("ext2", ext2);
		datamap.put("payType", payType);
		datamap.put("bankId", bankId);
		datamap.put("redoFlag", redoFlag);
		datamap.put("pid", pid);

		try {
			// 发送扫码请求报文
			log.info("请求报文：" + datamap);
			String str = MapUtils
					.getGatewayForm(
							myProp.getProperty("pay_url"),
							datamap, "");
			backMap.put("busContent", str);
			backMap.put("contentType", MsgCT.DATA_TYPE_HTML);
			log.debug("返回数据：" + backMap);
			return backMap;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("渠道调用失败:" + e);
		}
		return null;
	}

	/**
	 * 提现（代付）
	 * 
	 * @param param
	 * @return
	 */
	private Map<String, Object> withdrawal(Map<String, Object> param) {
		log.info("[快钱]代付接口:[{}]", param);
		Map<String, Object> backMap = new HashMap<String, Object>();
		backMap.put("casOrdNo", ObjectUtil.getString(param.get("casOrdNo")));
		backMap.put("ordStatus", MsgCT.CAS_ORD_STATUS_01);
		String signKey = (String) param.get("merKey");
		signKey = myProp.getProperty("w_mer_key");
		String TMercId = (String) param.get("TMercId");
		// 初始化参数
		String timestamp = new Date().getTime() / 1000 + ""; // 时间
		try {
			// 客户编号所对应的密钥。。在账户邮箱中获取
			String key = signKey;
			// 城市,中文字符 主要只需要城市名，不需要省份名。也不要带"市""自治区（县）"等
			String province_city = ObjectUtil.getString(param.get("bankProvince"));
			// 银行卡开户行的名称
			String kaihuhang = ObjectUtil.getString(param.get("bankName"));
			// 收款人姓名,收款人的姓名，必须与银行卡账户名相同
			String creditName = ObjectUtil.getString(param.get("accountName"));
			// 银行卡号
			String bankCardNumber = ObjectUtil.getString(param.get("cardNo"));
			// 交易金额 整数或小数 小数为两位以内 但小数点的最后一位不能为0 如：0.10不行。单位为人民币元
			String amount = MoneyUtils.toStrYuan(ObjectUtil.getString(param
					.get("netrecAmt")));
			amount = rVal(amount);
			// 交易备注
			String description = "API代付";
			// 订单号
			String orderId = ObjectUtil.getString(param.get("casOrdNo"));
			// 组合字符串。。必须按照此顺序组串
			String macVal = bankCardNumber + amount + orderId + key;
			String mac = MD5Util.md5Hex(macVal.getBytes("gb2312"))
					.toUpperCase();
			String bankName = "";
			// 银行名称 请填写银行的标准名,详见接口文档
//			String bankName = "";
			String bankCode = ObjectUtil.getString(param.get("bankCode"));
			// 根据提现银行编号获得配置的第三方银行编号
			if (StringUtils.isNotEmpty(bankCode)) {
				try {
					Map<String, Object> queryMap = new HashMap<String, Object>();
					queryMap.put("channel", "KuaiQian");
					queryMap.put("binBankCode", bankCode);
					List<Map<String, Object>> thirdBankList = unionfitService
							.getThirdBankInfo(queryMap);
					if (thirdBankList != null && !thirdBankList.isEmpty()) {
						Map<String, Object> thirdBankMap = thirdBankList.get(0);
						bankCode = MapUtils.getString(thirdBankMap,
								"thirdBankCode", bankCode);
						bankName = MapUtils.getString(thirdBankMap, "bankName",
								"");
						log.info("获取第三方银行对照信息完成 [bankCode={}] [bankName={}]",
								bankCode, bankName);
					} else {
						backMap.put("ordStatus", MsgCT.ORDSTATUS_FAIL);
						backMap.put("msg", "暂不支持该银行!");
						backMap.put("busContent", "暂不支持该银行!");
						backMap.put("resJson", "{\"msg\":\"不支持的银行编码["
								+ bankCode + "]\"}");
						return backMap;
					}
				} catch (Exception e) {
					log.error("获取第三方银行对照信息异常:" + e.getMessage(), e);
					return backMap;
				}
			}

			BankRequestBean requestBean = new BankRequestBean();
			requestBean.setProvince_city(province_city);
			requestBean.setBankName(bankName);
			requestBean.setKaihuhang(kaihuhang);
			requestBean.setCreditName(creditName);
			requestBean.setBankCardNumber(bankCardNumber);
			requestBean.setAmount(Double.parseDouble(amount));
			requestBean.setDescription(description);
			requestBean.setOrderId(orderId);
			requestBean.setMac(mac);
			log.info("请求传参：[{}]",requestBean);
			BankRequestBean[] queryArray = new BankRequestBean[1];
			queryArray[0] = requestBean;

			String merchant_id = TMercId;
			String merchant_ip = "";
			BatchPayServiceLocator locator = new BatchPayServiceLocator();
			BankResponseBean[] responseBean = new BankResponseBean[1];
			responseBean = locator.getBatchPayWS().bankPay(queryArray,
					merchant_id, merchant_ip);
			if (responseBean[0].isResultFlag()) {
				backMap.put("ordStatus", MsgCT.CAS_ORD_STATUS_06);
			}
			log.info("responseBean:" + responseBean[0].toString());
			//

		} catch (Exception e) {
			e.printStackTrace();
			log.error("渠道调用失败:" + e);
		}
		return backMap;
	}
	
	
	/**
	 * 提现（代付）查询
	 * 
	 * @param param
	 * @return
	 */
	private Map<String, Object> withdrawalQuery(Map<String, Object> param) {
		log.info("[快钱]代付查询接口:[{}]", param);
		Map<String, Object> backMap = new HashMap<String, Object>();
		backMap.put("casOrdNo", ObjectUtil.getString(param.get("casOrdNo")));
		backMap.put("ordStatus", MsgCT.CAS_ORD_STATUS_01);
		String signKey = (String) param.get("merKey");
		signKey = myProp.getProperty("w_mer_key");
		String TMercId = (String) param.get("TMercId");
		// 初始化参数
		String timestamp = new Date().getTime() / 1000 + ""; // 时间
		
		String orderId = ObjectUtil.getString(param.get("casOrdNo")); //平台创建的订单号
		
		try {
			//queryDeal
			QueryRequestBean bean = new QueryRequestBean();
			bean.setQueryType("bankPay"); // 固定选择值:simplePay，bankPay  simplePay 代表付款到快钱账户 bankPay 代表付款到银行账户
			bean.setOrderId(orderId);
			bean.setDealId("0"); // 固定选择值:0、1 / 0 代表按商户订单号查询; 1 代表按交易时间范围查询，
			String merchant_id = TMercId;
			String merchant_ip = "";
			BatchPayServiceLocator locator = new BatchPayServiceLocator();
			QueryResponseBean[] responseBean = new QueryResponseBean[1];
			responseBean = locator.getBatchPayWS().queryDeal(bean, merchant_id, merchant_ip);
			log.info("快钱返回：[{}]",responseBean[0]);
					
			if (null!=responseBean && "111".equals(responseBean[0].getDealStatus())) {
				backMap.put("ordStatus", MsgCT.CAS_ORD_STATUS_07);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("渠道调用失败:" + e);
		}
		return backMap;
	}
	

	/**
	 * 查询支付状态
	 * 
	 * @param param
	 * @return
	 */
	private Map<String, Object> queryPayStatus(Map<String, Object> param) {
		log.info("[快钱]查询支付状态接口:[{}]", param);
		Map<String, Object> backMap = new HashMap<String, Object>();
		backMap.put("ordStatus", MsgCT.ORDSTATUS_PROCESSING);
		// 初始化参数
		String timestamp = new Date().getTime() / 1000 + ""; // 时间
		String signKey = (String) param.get("merKey");
		signKey = myProp.getProperty("pay_q_mer_key");
		String TMercId = (String) param.get("TMercId");
		try {
			// 固定参数
			TreeMap<String, String> postmap = new TreeMap<String, String>(); // 请求参数的map
			postmap.put("open_id", (String) param.get("TMercId"));
			postmap.put("timestamp", timestamp);

			String orderId = (String) param.get("prdOrdNo");

			QueryRequestBean requestBean = new QueryRequestBean();
			requestBean.setDealId("0");
			requestBean.setQueryType("simplePay"); // simplePay 代表付款到快钱账户
													// bankPay 代表付款到银行账户
			requestBean.setOrderId(orderId);
			String merchant_id = TMercId;
			String merchant_ip = "";
			BatchPayServiceLocator locator = new BatchPayServiceLocator();
			QueryResponseBean[] responseBean = new QueryResponseBean[1];
			responseBean = locator.getBatchPayWS().queryDeal(requestBean,
					merchant_id, merchant_ip);
			System.out.println("responseBean:" + responseBean[0].toString());
		} catch (Exception e) {
			e.printStackTrace();
			log.error("渠道调用失败:" + e);
		}
		return backMap;
	}
	
	/**
	 * 查询支付状态
	 * 
	 * @param param
	 * @return
	 */
	private Map<String, Object> queryPayStatus2(Map<String, Object> param) {
		log.info("[快钱]查询支付状态接口:[{}]", param);
		Map<String, Object> backMap = new HashMap<String, Object>();
		backMap.put("ordStatus", MsgCT.ORDSTATUS_PROCESSING);
		// 初始化参数
		String timestamp = new Date().getTime() / 1000 + ""; // 时间
		String signKey = (String) param.get("merKey");
		signKey = myProp.getProperty("pay_q_mer_key");
		String TMercId = (String) param.get("TMercId");
		try {
			// 固定参数
			TreeMap<String, String> postmap = new TreeMap<String, String>(); // 请求参数的map
			postmap.put("open_id", (String) param.get("TMercId"));
			postmap.put("timestamp", timestamp);

			String prdOrdNo = (String) param.get("prdOrdNo");

			String inputCharset = "1";
			String version = "v2.0";
			String signType = "1";
			String merchantAcctId = TMercId +"01";//本参数用来指定接收款项的人民币账号
			String queryType = "0";
			String queryMode = "1";
			String startTime = "";
			String endTime = "";
			String requestPage = "";
			String orderId = prdOrdNo;
			String key = signKey;

			String signMsgParam="";
			signMsgParam = appendParam(signMsgParam,"inputCharset",inputCharset);
			signMsgParam = appendParam(signMsgParam,"version",version);
			signMsgParam = appendParam(signMsgParam,"signType",signType);
			signMsgParam = appendParam(signMsgParam,"merchantAcctId",merchantAcctId);
			signMsgParam = appendParam(signMsgParam,"queryType",queryType);
			signMsgParam = appendParam(signMsgParam,"queryMode",queryMode);
			signMsgParam = appendParam(signMsgParam,"startTime",startTime);
			signMsgParam = appendParam(signMsgParam,"endTime",endTime);
			signMsgParam = appendParam(signMsgParam,"requestPage",requestPage);
			signMsgParam = appendParam(signMsgParam,"orderId",orderId);
			signMsgParam = appendParam(signMsgParam,"key",key);

			String signMsg = MD5Util.md5Hex(signMsgParam.getBytes("utf-8")).toUpperCase();

			GatewayOrderQueryRequest queryRequest = new GatewayOrderQueryRequest();
			queryRequest.setInputCharset(inputCharset);
			queryRequest.setVersion(version);
			queryRequest.setSignType(Integer.parseInt(signType));
			queryRequest.setMerchantAcctId(merchantAcctId);
			queryRequest.setQueryType(Integer.parseInt(queryType));
			queryRequest.setQueryMode(Integer.parseInt(queryMode));
			queryRequest.setOrderId(orderId);
			queryRequest.setStartTime(startTime);
			queryRequest.setEndTime(endTime);
			queryRequest.setRequestPage(requestPage);
			queryRequest.setSignMsg(signMsg);

			GatewayOrderQueryServiceLocator locator = new GatewayOrderQueryServiceLocator();
			GatewayOrderQueryResponse queryResponse = locator.getgatewayOrderQuery().gatewayOrderQuery(queryRequest);
			GatewayOrderDetail[] orders = queryResponse.getOrders();
			if (null!=orders && orders.length>0) {
				if ("10".equals(orders[0].getPayResult())) {
					backMap.put("ordStatus", MsgCT.ORDSTATUS_SUCCESS);
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("渠道调用失败:" + e);
		}
		return backMap;
	}
	
	public static String rVal(String m){
		Double mainWastage = Double.parseDouble(m);
		//主材损耗费取整
		if(mainWastage.intValue()-mainWastage==0){//判断是否符合取整条件
			return mainWastage.intValue()+"";
		}else{
			return String.valueOf(mainWastage);
		}
	}

	public static void main(String[] args) throws Exception {
	
		KuaiQianServiceImpl service = new KuaiQianServiceImpl();

		Map<String, Object> merParam = new HashMap<String, Object>();

		// merParam.put("TMercId", "73b24f53ffc64486eb40d606456fb04d");
		// merParam.put("merKey", "7386072b1f94fdd7acaae83cd0f0f1c1");

		// 杭州灵峰教育后勤管理有限公司宁波第二分公司
		// 887899091商户号
		merParam.put("TMercId", "10210068232");
		merParam.put("merKey", "WFRXU4CZQZXNA52Z");

		{
			// 支付
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("rtrcod", "pay");

			param.putAll(merParam);
			param.put("payAmt", "1000");
			param.put("rateType", "0601");
			param.put("prdOrdNo", System.currentTimeMillis() + "");
//			 Map<String, Object> resMap = service.execute(param);
//			 System.out.println("返回结果:" + resMap);
		}

		{
			// 订单明细查询
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("rtrcod", "pay_que");
			param.putAll(merParam);
			param.put("prdOrdNo", "1539916595201");
			param.put("merKey", "FHEDLCQ9CDAHHGJS");
			
//			Map<String, Object> resMap = service.execute(param);
//			System.out.println("返回结果:" + resMap);
		}

		{
			// 代付
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("rtrcod", "withdrawal");
			param.put("cityName", "北京");
			param.put("kaihuhang", "北京朝阳门支行");
			param.put("bankName", "招商银行");
			param.put("custName", "刘帮");
			param.put("cardNo", "6226090110382849");
			param.put("netrecAmt", "300");
			param.put("casOrdNo", System.currentTimeMillis() + "");
			param.putAll(merParam);
			param.put("merKey", "FHEDLCQ9CDAHHGJS");
//			 Map<String, Object> resMap = service.execute(param);
//			 System.out.println("返回结果:" + resMap);
		}
		
		{
//			3234208623
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("rtrcod", "withdrawalQuery");

			param.putAll(merParam);
			param.put("casOrdNo", "1540992566957");
//			param.put("rateType", "0601");
//			param.put("prdOrdNo", System.currentTimeMillis() + "");
			 Map<String, Object> resMap = service.execute(param);
			 System.out.println("返回结果:" + resMap);
		}

	}

}
